package com.lg.jvm.gc;

import com.lg.jvm.User;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 测试 finalize 终结期引用的对象自救
 *
 * @author Gang.Li
 * @date 2021/6/4 12:00
 */
public class OOMTest {
    public static List<Object> list = new ArrayList<>();

    // JVM设置
    // -Xms10M -Xmx10M -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=D:\jvm.dump
    // jvisualVM 测试
    public static void main(String[] args) {

        int i = 0;
        int j = 0;
        while (true) {
            list.add(new User(i++, UUID.randomUUID().toString()));
            new User(j--, UUID.randomUUID().toString());
        }
    }


    //User类需要重写finalize方法
    @Override
    protected void finalize() throws Throwable {
        OOMTest.list.add(this);
        System.out.println("关闭资源，userid="  + "即将被回收");
    }
}